package UILayer;
import java.util.Scanner;
import CtrLayer.*;
import ModLayer.*;
import java.util.ArrayList;

/**
 * Write a description of class SaleUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SaleUI
{
    private SaleCtr saleCtr;
    private ProductCtr productCtr;
    private TimeAndCheck time;
    private PersonCtr personCtr;
    
    public SaleUI()
    {
        saleCtr = saleCtr.getInstance();
        time = time.getInstance();
        productCtr = productCtr.getInstance();
        personCtr = personCtr.getInstance();
    }

    public void start()
    {
        boolean finished = false; 
        int counterOfDeleted = saleCtr.deleteOutDatedWishlist();
        while(!finished)
        {
            printMenu();
            if(counterOfDeleted > 0)
                System.out.println("\nOutdated wish list was deleted - "+counterOfDeleted);
            switch(reader(4)) 
            { 
                case "0": finished = true; break;
                case "1": removeSale(); break;
                case "2": createSale(); break;
                case "3": checkoutMenu(); break;
                case "4": searchSale(); break;
            } 
        } 
    }

    private void printMenu()
    { 
        System.out.println("\f****Sales Menu****");
        System.out.println("\nWhat would you like to do?"); 
        System.out.println("(0) Back."); 
        System.out.println("(1) Remove wishlist."); 
        System.out.println("(2) Make wishlist."); 
        System.out.println("(3) Checkout.");
        System.out.println("(4) Show all wishlists.");
        System.out.println("Make your choice: ");
    }

    public void checkoutMenu()
    {
        System.out.println("Type in the id of the sale you wish to delete.");
        String read = reader2();
        if(read != "0")
        {
            if(saleCtr.findSale(read)!=null)
            {
                checkout(read);
                System.out.println("Checkout successful");
            }
            else
                System.out.println("No such Sale was found.");
        }
    }

    public void checkout(String id)
    {
        System.out.println("Type in the discount percentage you have negociated.");
        double discount = Double.parseDouble(reader2());
        saleCtr.checkout(id, discount);
        Sale s = saleCtr.findSale(id);
        if(saleCtr.isCustomerRegistered(s))
        {
            System.out.println("\nName of company: "+personCtr.getInfoCompany("2"));
            System.out.println("Address of company: "+personCtr.getInfoCompany("0"));
            System.out.println("Phone number of company: "+personCtr.getInfoCompany("3"));
            System.out.println("Email of company: "+personCtr.getInfoCompany("1"));
            System.out.println("Fax number of company: "+personCtr.getInfoCompany("5"));
            System.out.println("Bank account number of company: "+personCtr.getInfoCompany("4"));
        }
        System.out.println("Customer id is " + s.getIDCustomer());
        System.out.println("Employee id is " + s.getIDEmployee());
        ArrayList<String> pList = s.getProductList();
        ArrayList<Integer> qList = s.getQuaList();
        for(int i = 0; i<= pList.size()-1; i++)
        {
            printProduct(productCtr.findProduct(pList.get(i)));
            System.out.print(" " + qList.get(i));
        }
        saleCtr.removeSale(id);
    }

    public void createSale()
    {
        boolean finished = false; 
        System.out.println("\f*** Add sale ***");
        String id = time.getNoForCustomer();
        System.out.println("\nType in id code of employee.");
        String c = reader2();
        System.out.println("\nType in id of customer or bank account info.");
        String e = reader2();
        if(saleCtr.createSale(id, c, e))
        {
            System.out.println("\fSale was created.");
            System.out.println("\nAdding products for sale if you want skip press 0 at any time.");
            addProduct(id);
            System.out.println();
            System.out.println();
            updateSale(id);
        }
        else
        {
            System.out.println("Couldn't find employee");
        }
    }

    public void updateSale(String id)
    {
        if(saleCtr.findSale(id)!=null)
        {
            System.out.println("\nWhat would you like to do with this sale?");
            System.out.println("(0) Save."); 
            System.out.println("(1) Checkout."); 
            System.out.println("(2) Order."); 
            System.out.println("(3) Scrap.");
            System.out.println("Make your choice: ");
            switch(reader(3)) 
            {
                case "0": break;
                case "1": checkout(id); break;
                
                case "2": 
                System.out.println("Are you sure you wish to make an order?");
                if(reader2().startsWith("y"))
                    makeOrder(saleCtr.findSale(id), saleCtr.getSum(saleCtr.findSale(id), 0));
                else
                    System.out.println("Sale will be saved until further notice.");
                break;
                
                case "3": saleCtr.removeSale(id); break;
            }
        }
        else
            System.out.println("Sale Id not found.");
    }

    public void makeOrder(Sale s, double sum)
    {
        System.out.println("How much is the transport?");
        String p = reader2();
        
        while(time.checkStringToDouble(p)==0)
        {
            System.out.println("\nTry once again?");
            p = reader2();
        }
        System.out.println("How long will the delivery take?");
        String duration = reader2();
        while(time.checkStringToDouble(duration)==0)
        {
            System.out.println("\nTry once again?");
            duration = reader2();
        }
        if(!saleCtr.isCustomerRegistered(s))
        {
            System.out.println("What is the adress? ");
            String adress = reader2();
            System.out.println("Adress: " + adress);
        }
        else
            System.out.println("Adress: " +personCtr.getInfoCustomer(s.getIDCustomer(), "0"));
        
        System.out.println("Customer id is: " + s.getIDCustomer());
        System.out.println("Employee id is: " + s.getIDEmployee());
        ArrayList<String> pList = s.getProductList();
        ArrayList<Integer> qList = s.getQuaList();
        for(int i = 0; i<= pList.size()-1; i++)
        {
            printProduct(productCtr.findProduct(pList.get(i)));
            System.out.print(" " + qList.get(i));
        }
        
        System.out.println("\nDuration: " + duration);
        
        System.out.println("Sum: " +sum+ " DKK");
        System.out.println("Transport: " +p);
        double total = sum + Integer.parseInt(p);
        System.out.println("Total cost: " +total);
        pause();
    }

    public void removeSale()
    {
        System.out.println("\f***Remove a sale***");
        System.out.println("Enter sale id:");
        String read = reader2();
        if(read != "0")
        {
            if(saleCtr.findSale(read)!=null)
            {
                saleCtr.removeSale(read);
                System.out.println("Sale successfully deleted.");
            }
            else
                System.out.println("No such Sale was found.");
        }
    }

    private void removeProduct(String id, String p)
    {
        saleCtr.removeProduct(id, p);
    }

    private void addProduct(String id)
    {
        boolean finished = false;
        boolean ok = false;
        String p = null;
        String pr = null;
        while(!finished)
        {
            System.out.println("Enter product id or '1' to delete the previous product.");
            ok = false;
            p = null;
            p = reader2();
            while(!p.equals("0"))
            {
                if(p.equals("1"))
                    {removeProduct(id, pr); break;}
                if(productCtr.findProduct(p)!=null)
                    {ok=true; break;}
                else
                    System.out.println("Not a valid item.");
                p = reader2();
            }
            if(ok == true)
            {
                System.out.println("Enter quantity of that item:");
                int qua = Integer.parseInt(reader2());
                if(qua == 0)
                    finished = true;
                printProduct(productCtr.findProduct(p));
                saleCtr.addProduct(id, p, qua);
            }
            if(p.equals("0"))
                finished = true;
        }
    }

    public void searchSale()
    {
        for(Sale x : saleCtr.searchSale())
        {
            printSale(x);
        }
        System.out.println("Write the ID of the one you wish to update:");
        updateSale(reader2());
    }

    public void printSale(Sale s)
    {
        System.out.println("Sale ID: " + s.getID());
        System.out.println("Customer ID: " + s.getIDCustomer());
        System.out.println("Employee ID: " + s.getIDEmployee());
        ArrayList<String> pList = new ArrayList<String>();
        pList = s.getProductList();
        ArrayList<Integer> qList = new ArrayList<Integer>();
        qList = s.getQuaList();
        for(int i = 0; i<= pList.size()-1; i++)
        {
            System.out.print(" " + qList.get(i));
        }
    }

    private String reader2()
    { 
        Scanner command = new Scanner(System.in); 
        System.out.print(">> "); 
        String c = command.nextLine(); 
        return c;
    }

    private String reader(int limitIndex)
    { 
        final String[] validNumbers = {"0","1","2","3","4","5","6","7","8","9"};
        Scanner command = new Scanner(System.in);
        String c = null;
        boolean finished = false;
        while(!finished)
        { 
            System.out.print("\n> "); 
            c = command.nextLine();
            for(int i=0; i<=limitIndex; i++)
            {
                if(validNumbers[i].equals(c)) 
                    finished = true;
            }
            if(finished == false) 
            {
                System.out.print("\nTry once again");
                c = null;
            }
        }
        return c;
    }

    public void printProduct(Product pr)
    {
        System.out.println("*****");
        System.out.println("Name of the product: "+pr.getName());
        System.out.println("ID of the product: "+pr.getID());
        System.out.println("Description of the product: "+pr.getDescription());
        System.out.println("Number in stock: " +pr.getStock());
        System.out.println("Number in Kitchen: " +pr.getStockKit());
        System.out.println("Number in Bathroom: " +pr.getStockBath());
        System.out.println("Number in the DIY centre: " +pr.getStockDIY());
        System.out.println("Price of the product: " +pr.getPrice(pr.getDiscount())+ " DKK");
        System.out.print("Available to ");
        if(pr.getSellable() == true)
            System.out.print("buy ");
        if(pr.getLeaseable() == true&&pr.getSellable() == true)
            System.out.print("and ");
        else
            System.out.print(".");
        if(pr.getLeaseable() == true)
            System.out.print("lease.");
        System.out.println();
        System.out.println();
    }
    
    private void pause()
    {
        boolean finished = false; 
        while(!finished)
        {
            System.out.println("\nPress something to back.");
            if(reader2().startsWith(""))
                finished = true;
        }
    }
}
